<?php
/* explain:
 * Project:     
 * File:        Job.php
 *
 * @ link 		http://www.../lib/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2007  Ling Deming
 * @ author 	ivan.ling  Ling Deming
 * @ version 	1.0
 *
 */
include_once("utils/CommonUtils.php");

class OperateImpl extends CommonUtils
{
	public $conHandle;
	public $errorInfo;
	public static $SelfFileName;

	function __construct($DBTag = 'system')
	{
		parent::__construct();
		
		$this->DBTag 	= $DBTag;
	}

	/* get data via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function getById($id)
	{
		$sql = "SELECT * FROM operation WHERE id = $id";
		
		return $this->getRow($this->DBTag, $sql);
	}

	/* get data via groupid and menuid
	 *
	 * @param  int    id
	 * @return operate array
	 */
	
	public function getAuthOperates($groupid, $menuid)
	{
		if($groupid == 1){
			$sql = "SELECT id FROM operation";
			
			return $this->getColumns($this->DBTag, $sql);				
		}else{
			$sql = "SELECT operate FROM group_authority WHERE menuid = '$menuid' AND groupid = '$groupid'";
			
			$operate = $this->getColumn($this->DBTag, $sql);
			
			return explode(',', $operate);
		}		
	}
	
	/* 
	 *
	 * @param  string  	name 	
	 * @param  string 	namecn 
	 * @return boolean 	true or fail
	 * 
	 */
	public function add($arrPopedom = array(), $name)
	{
		$sql = "INSERT INTO operation SET name = '$name'";
		
		return $this->insert($this->DBTag, $sql);
	}
		
	/* modify menu
	 *
	 * @param  string 	id			modify menu id
	 * @param  string 	strOrders	
	 * @param  string  	name 	
	 * @param  string 	namecn 
	 * @return boolean 	true or fail
	 */
	public function edit($arrPopedom = array(), $id, $name)
	{
		$sql = "UPDATE operation SET name = '$name', WHERE id='$id'";
		
		return $this->update($this->DBTag, $sql);
	}
			
	/* delete menu
	 *
	 * @param  int    	id
	 * @param  return 	boolean
	 */
	public function delete($arrPopedom = array(), $id)
	{		
		$sql = "DELETE FROM operation WHERE id = $id";
		
		return $this->update($this->DBTag, $sql);
	}
	
	/* list all custom condition data
	 *
	 * @param  string condition
	 * @return mixed  false: sql error  array: news info
	 */
	public function getArray($where = '')
	{
		$sql = "SELECT id, name, name as viewName FROM operation $where";
        
        return $this->getRows($this->DBTag, $sql);
	}
	
	/* operation list
	 *
	 * @ param  return 	array		data array
	 */
	public function listOperation()
	{
		$conn 		= null;
		$arrback 	= array();
			
		$arrID		= array();
		$arrName 	= array();
		
		$sql = "SELECT id, name FROM operation";
		
		try{
			$conn 	= $this->getConnection($this->DBTag);
			$rs  	= mysql_query($sql, $conn);
			
			while(is_array($arrcache = mysql_fetch_array($rs))){
				$arrID[] 	= $arrcache['id'];
				$arrName[] 	= $arrcache['name'];
			}
			
			$arrback['id'] 		= $arrID;
			$arrback['name'] 	= $arrName;			
	 	}catch(Exception $e){
				$this->message = $e->getMessage();			
		}
			
		$this->freeConnection($this->DBTag, $conn);
		
		return $arrback;
	}

	public function arrGetOPInfo($arrOperate)
	{
		$arrOperateInfo = array();
		
		if(count($arrOperate) > 0){
			$strQuery = $_SERVER["REQUEST_URI"];
			$arrUrlInfo 	= pathinfo($strQuery);
			Operation::$SelfFileName = $arrUrlInfo['filename'];
			
			$arrOperateAllInfo = $this->listFromCustom($arrOperate, " WHERE id in (".implode(",", $arrOperate).")");
			
			$i = 0; 
			foreach($arrOperateAllInfo as $key => $value){
				$strFileName = Operation::$SelfFileName."_".$value['name'].".php";
		
				if(file_exists($strFileName)){
					$arrOperateInfo[$i]['name'] 		= $value['name'];
					$arrOperateInfo[$i]['viewName'] 	= $value['name'];
					
					$i++;
				}
			}
		}
		
		return $arrOperateInfo;
	}
	
	public function strGetSelfFileName()
	{
		return Operation::$SelfFileName;
	}
	
	public function lists($where, $order, $page, $discount, $url = '')
	{
		return $this->getPageRows($this->DBTag, '*', "operation $where", $order, $page, $discount, $url);	
	}
}
?>
